import baseCofnig from '../config.js'
var delayedTime = 0
const utils = {
	//获取访问地址
	/**
	 * @param {string} str 可不传
	 */
	getBaseUrl(str) {
		if (str == undefined) {
			str = baseCofnig.request_url
			// #ifdef H5
			if (!str) {
				str = location.origin
			}
			// #endif
		}
		return str
	},
	//查看消息
	/**
	 * @param {string} message 消息
	 * @param {Function} callback 回调
	 * @param {boolean} mask 是否遮罩
	 * @param {int} time 遮罩时间
	 */
	showMessage(message, callback, mask, time) {
		if (mask == undefined) {
			mask = true
		}
		if (time == undefined) {
			time = baseCofnig.duration
		}
		// #ifdef MP
		if (message.length > 7) {
			uni.showModal({
				title: "提示",
				content: message,
				showCancel: false,
				complete() {
					setTimeout(function() {
						if (callback) {
							callback()
						}
					}, time)
				}
			})
		} else {
			uni.showToast({
				title: message,
				icon: 'none',
				mask: mask,
				duration: time,
				complete() {
					setTimeout(function() {
						if (callback) {
							callback()
						}
					}, time)
				}
			})
		}
		// #endif
		// #ifndef MP
		uni.showToast({
			title: message,
			icon: 'none',
			mask: mask,
			duration: time,
			complete() {
				setTimeout(function() {
					if (callback) {
						callback()
					}
				}, time)
			}
		})
		// #endif

	},
	/**延迟执行
	 * @param {Object} callback
	 * @param {Object} time
	 */
	delayedHand(callback, time) {
		if (time == undefined) {
			time = baseCofnig.delayed
		}
		delayedTime = (new Date()).getTime()
		setTimeout(function() {
			let ntime = (new Date()).getTime()
			if (ntime > (delayedTime + time - 50)) {
				if (callback) {
					callback()
				}
			}
		}, time)
	},
	//倒计时
	/**倒计时
	 * @param {Function} callback 回调函数
	 * @param {int} time 回调次数
	 */
	countdown(callback, time) {
		if (time == undefined) {
			time = 60
		}
		time--;
		if (callback) {
			callback(time)
		}
		if (time > 0) {
			setTimeout(function() {
				utils.countdown(callback, time)
			}, 1000)
		}
	},
	/**隐藏字符串
	 * @param {string|number} mobile 手机号码
	 * @param {string|number} char 字符串时代表拼接的字符,数字代表开始截取的位置
	 * @param {number} start char为字符串，这个字段为开始截取位置，char为数字，这个字段是截取长度
	 * @param {number} length 字符串替换长度
	 */
	hideStr(str, char, start, length) {
		if ((typeof char) == "number") {
			if (start == undefined) {
				start = char
			} else {
				length = start
				start = char
			}
			char = "*"
		}
		let phone = /^1[3456789][0-9]{9}$/
		if (phone.test(str)) {
			return utils.hideMobile(str, char, 3, 4)
		}
		if (char == undefined) {
			char = "*"
		}
		if (start == undefined) {
			start = 1
		}
		if (length == undefined) {
			length = 1
		}
		char = char.repeat(length)
		str = str.substr(0, start) + char + str.substr(start + length)
		return str
	},
	/**隐藏手机号码
	 * @param {string|number} mobile 手机号码
	 * @param {string|number} char 字符串时代表拼接的字符,数字代表开始截取的位置
	 * @param {number} start char为字符串，这个字段为开始截取位置，char为数字，这个字段是截取长度
	 * @param {number} length 字符串替换长度
	 */
	hideMobile(mobile, char, start, length) {
		if ((typeof char) == "number") {
			if (start == undefined) {
				start = char
			} else {
				// console.log(start);
				length = start
				start = char
			}
			char = "*"
		}
		if (char == undefined) {
			char = "*"
		}
		if (start == undefined) {
			start = 3
		}
		if (length == undefined) {
			length = 4
		}
		if ((typeof mobile) == 'number') {
			mobile = mobile.toString()
		}
		char = char.repeat(length)
		return mobile.substr(0, start) + char + mobile.substr(start + length)
	},
	/**使用cdnurl拼接图片路径
	 * @param {string} str
	 */
	cdnImg(str) {
		if (str && str.indexOf("http") == -1) {
			if (str.indexOf("data:image/svg+xml;base64,") != -1) {
				return str;
			}
			let cdn_url = baseCofnig.request_url
			if (baseCofnig.img_url) {
				cdn_url = baseCofnig.img_url
			}
			// console.log(cdn_url+str);
			return cdn_url + str;
		} else {
			return str;
		}

	},
	/**多图拼接cdn路径
	 * @param {string|Array|object} str
	 */
	cdnImgs(str) {
		if ((typeof str) == 'string') {
			// console.log(str.indexOf(','));
			if (str.indexOf(',') != -1) {
				str = str.split(',')
				for (var i in str) {
					str[i] = utils.cdnImg(str[i])
				}
				return str;
			} else {
				return utils.cdnImg(str);
			}
		} else {
			for (var i in str) {
				str[i] = utils.cdnImg(str[i])
			}
			return str
		}
	},
	/**
	 * 跳转地址
	 * @param {String} str 跳转地址
	 * @param {Function} func 回调
	 */
	goToUrl(str, func) {
		if (str.indexOf("http") != -1) {
			if (func) {
				func(str)
			} else {
				// #ifdef APP-PLUS
				plus.runtime.openURL(str)
				// #endif
				// #ifdef H5
				location.href = str
				// #endif
				// #ifdef MP
				uni.navigateTo({
					url: "/pages/webview/webview?url=" + encodeURIComponent(str)
				})
				// #endif
			}
		} else {
			if (func) {
				func(str)
			} else {
				uni.navigateTo({
					url: str,
					fail() {
						uni.switchTab({
							url: str
						})
					}
				})
			}
		}
	},
	/**跳转到webview
	 * @param {String} url 跳转地址
	 * @param {String} title 头部显示
	 */
	goWebView(url, title) {
		if (title == '' || title == undefined) {
			uni.navigateTo({
				url: '/pages/webview/webnotheader?url=' + encodeURIComponent(url)
			})
		} else {
			uni.navigateTo({
				url: '/pages/webview/webhasheader?url=' + encodeURIComponent(url) + "&title=" + title
			})
		}
	},
	//上传图片
	UploadImages(url, callback, config) {
		if (config == undefined) {
			config = {
				count: 1,
				sourceType: ['camera', 'album']
			}
		}
		let header = config.heaer||{}
		let token = uni.getStorageSync('token')
		if(token){
			header.token = token
		}
		url = baseCofnig.request_url+url
		config.success = function(res) {
			var tempFilePaths = res.tempFilePaths;
			for (var i in tempFilePaths) {
				console.log(i);
				uni.uploadFile({
					url: url,
					header:header,
					filePath: tempFilePaths[i],
					name: config.name || 'file',
					formData: config.formData || {},
					success(response) {
						if (response.statusCode == 200) {
							if (callback) {
								callback(response.data)
							}
						}
					},
					fail(res) {
						console.log("上传失败", res);
					}
				})
			}
		}
		config.fail = function(re) {
			console.log("读取文件失败", re);
		}
		console.log(config);
		uni.chooseImage(config)
	},
	//html转view
	htmlToView(html) {
		let imgUrl = baseCofnig.img_url;
		if (imgUrl == "") {
			imgUrl = baseCofnig.request_url
		}

		html = html.replace(/src=\"\/uploads/g, "src=\"" + imgUrl + "/uploads")
		html = html.replace(/style="width: [\d]{1,10}px;"/g, '')
		// console.log(html);
		return html;
	},
	//判断是微信还是
	determineBrowser() {
		return new Promise((resolve, reject)=>{
			uni.getSystemInfo({
				success: function(res) {
					console.log(res);
					const platform = res.platform.toLowerCase();
					console.log(platform);
					// resolve(platform)
					if (platform === 'devtools') {
						if(/AlipayClient/.test(window.navigator.userAgent)){
							resolve("alipay")
						}else{
							resolve('h5');
						}
					
					} else if (platform === 'ios' || platform === 'android') {
						resolve(platform);
					} else if (platform === 'wechat') {
						resolve('wechat');
					} else {
						reject('无法确定当前环境');
					}
				},
				fail: function(error) {
					reject(error);
				}
			})
		})
	},
	//获取微信code
	getCode(){
		return new Promise((resolve, reject)=>{
			// #ifdef MP-WEIXIN
			wx.login({
				success(res){
					resolve(res.code)
				},
				fail(e) {
					reject(res)
				}
			})
			// #endif
			// #ifdef H5
			resolve("")
			// #endif
		})
	},
	//判断是否登录
	isLogin(getToken){
		if(getToken==undefined){
			getToken = false
		}
		let token = uni.getStorageSync('token');
		if(getToken){
			if(token){
				return token
			}
		}else{
			if(token){
				return true
			}
		}
		return false
	}
}
export default utils